<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$return['post']=array();
$return['paytypes']=array(
		'credit'=>'余额支付',
		'wechat'=>'微信支付',
		'alipay'=>'支付宝支付',
		'yinlian'=>'银联支付',
		'hdfk'=>'货到付款',
);
if($_RQ['op']=='main'){
	$return['params']=array('status'=>$_RQ['status']?:2);
	exi($return);
}
/*******op*******/
set_html('title','服务订单');
if($_RQ['op']=='list'){
	$worker = pdo_get('jy_wxshifu_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$params=array('shifuid'=>$worker['id'],'uniacid'=>$_SESSION['uniacid']);
	$_RQ['status']=$_RQ['status']?:2;
	if($_RQ['status']!=100){
		if($_RQ['status']==3){
			$params['status']=array(3,6);
		}else{
			$params['status']=intval($_RQ['status']);
		}
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=100;
	}
	if($_RQ['today']){
		$params['createtime >']=strtotime(date('ymd'));
	}
	//print_r($params);exit;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_wxshifu_order',$params,array($pindex,$psize),$total,array(),'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['orderinfo']=$this->model('model')->getorder($item,1);
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['statustitle']=$this->order_status[$item['status']];
			if($item['status']==5){
				$item['baobeimsg']=pdo_getcolumn('jy_wxshifu_baobei_log',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$item['id']),'msg');
			}
			$item['images']=$item['images']?json_decode($item['images'],true):array();
		}
	}
	$return['list']=$list;
	$shifus=array();
	//多商户用户，选择平台区域师傅
	$areas=pdo_getall('jy_wxshifu_area',array('store_id'=>0,'uniacid'=>$_SESSION['uniacid']));
	foreach($areas as $v){
				$users=pdo_getall('jy_wxshifu_member',array('group_id'=>$v['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				foreach($users as $uv){
					$shifus[]=$uv;
				}
	}
	$return['shifus']=$shifus;
	$return['isopenvoice']=$this->session('isopenvoice')?1:0;
	$return['url']=custom_url('jy_wxshifu/sao//fromuser='.$_SESSION['uid'],true);
	$return['params']=array('status'=>$return['status']);
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_wxshifu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['orderinfo']=$this->model('model')->getorder($order,1);
	$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
	$return['order']=$order;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_receive($order_id);
	exi('恭喜你完成一单！感谢你的努力！');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->pdo_update('order',array('status'=>5),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$order=pdo_get('jy_wxshifu_order',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('model')->uporder($order,5,true);
	exi('取消订单成功');
}
//转单
if($_RQ['op']=='change'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->up_sendlog($order_id,'',3);
	$order=pdo_get('jy_wxshifu_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	if($post['shifuid'] && $post['need_setshifu']){
		pdo_update('jy_wxshifu_order',array('shifuid'=>$post['shifuid'],'status'=>1),array('id'=>$order_id));
		$this->model('order')->create_sendlog($order_id,$post['shifuid']);
		$this->model('model')->uporder($order,1,true);
	}else{
		pdo_update('jy_wxshifu_order',array('shifuid'=>0,'status'=>0),array('id'=>$order_id));
		$this->model('model')->uporder($order,0,true);
	}
	exi('转单成功！');
}
//员工确认接单
if($_RQ['op']=='jieorder'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_checkget($order_id);
	exi('接单成功！');
}
//报备
if($_RQ['op']=='baobei'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	if($post['baobeitype']==3){
		$msg=$this->baobei_types3[$post['othertype']];
		if($post['remark']){
			$msg=$msg?$msg.'--':'';
			$msg .=$post['remark'];
		}
	}else{
		$msg=$this->baobei_types[$post['baobeitype']];
	}
	$this->model('order')->add_baobei($order_id,$msg);
	exi('报备成功！','','jy_wxshifu/worker_order/list/');
}
if($_RQ['op']=='setprice'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_upprice($post);
	exi('提交成功！','','jy_wxshifu/worker_order/list/');
}